package com.lh.project.controller;

import com.lh.project.common.BaseResponse;
import com.lh.project.common.ErrorCode;
import com.lh.project.common.ResultUtils;
import com.lh.project.common.UserHolder;
import com.lh.project.exception.BusinessException;
import com.lh.project.model.entity.User;
import com.lh.project.model.vo.UserVO;
import com.lh.project.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 用户接口
 *
 * @author lh
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;
    /**
     * 用户登录验证（如果没有该用户，则创建一个该用户，否则返回用户信息）
     * @param user
     * @param request
     * @return
     */
    @PostMapping("/post")
    public BaseResponse<User> getLoginUser(@RequestBody User user,HttpServletRequest request){
        System.out.println(user);
        User user1 = userService.getByOpenid(user);
        return ResultUtils.success(user1);
    }

    /**
     * 用户登录，将传过来的数据打包为User的格式，执行这个方法，就是当点击前端登录的时候
     * @param user
     * @return
     */
    @PostMapping("/login")
    public BaseResponse<String> login(@RequestBody User user){
        return userService.login(user);
    }

    /**
     *
     * @param stuNumber
     * @param httpServletRequest
     * @return
     */
    @PostMapping("bind")
    public BaseResponse<User>  bindStuNumber(@RequestBody Long stuNumber,HttpServletRequest httpServletRequest){
        System.out.println("传来的学号："+stuNumber);
        User user =  userService.bindStuNumber(stuNumber,httpServletRequest);
        return ResultUtils.success(user);
    }


    /**
     * 根据 id 获取用户
     *
     * @param
     * @param request
     * @return
     */
    @GetMapping("/get")
    public BaseResponse<UserVO> getUserById(HttpServletRequest request) {
        return ResultUtils.success(UserHolder.getUser());
    }



    // region 登录相关

    /**
     * 用户注册
     *
     * @param userRegisterRequest
     * @return
     */
//    @PostMapping("/register")
//    public BaseResponse<Long> userRegister(@RequestBody UserRegisterRequest userRegisterRequest) {
//        if (userRegisterRequest == null) {
//            throw new BusinessException(ErrorCode.PARAMS_ERROR);
//        }
//        String userAccount = userRegisterRequest.getUserAccount();
//        String userPassword = userRegisterRequest.getUserPassword();
//        String checkPassword = userRegisterRequest.getCheckPassword();
//        if (StringUtils.isAnyBlank(userAccount, userPassword, checkPassword)) {
//            return null;
//        }
//        long result = userService.userRegister(userAccount, userPassword, checkPassword);
//        return ResultUtils.success(result);
//    }

    /**
     * 用户登录
     *
     * @param userLoginRequest
     * @param request
     * @return
     */
//    @PostMapping("/login")
//    public BaseResponse<User> userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request) {
//        if (userLoginRequest == null) {
//            throw new BusinessException(ErrorCode.PARAMS_ERROR);
//        }
//        String userAccount = userLoginRequest.getUserAccount();
//        String userPassword = userLoginRequest.getUserPassword();
//        if (StringUtils.isAnyBlank(userAccount, userPassword)) {
//            throw new BusinessException(ErrorCode.PARAMS_ERROR);
//        }
//        User user = userService.userLogin(userAccount, userPassword, request);
//        return ResultUtils.success(user);
//    }

    /**
     * 用户注销
     *
     * @param request
     * @return
     */
//    @PostMapping("/logout")
//    public BaseResponse<Boolean> userLogout(HttpServletRequest request) {
//        if (request == null) {
//            throw new BusinessException(ErrorCode.PARAMS_ERROR);
//        }
//        boolean result = userService.userLogout(request);
//        return ResultUtils.success(result);
//    }

    /**
     * 获取当前登录用户
     *
     * @param request
     * @return
     */
//    @GetMapping("/get/login")
//    public BaseResponse<UserVO> getLoginUser(HttpServletRequest request) {
//        User user = userService.getLoginUser(request);
//        UserVO userVO = new UserVO();
//        BeanUtils.copyProperties(user, userVO);
//        return ResultUtils.success(userVO);
//    }


    // endregion

    // region 增删改查

    /**
     * 创建用户
     *
     * @param userAddRequest
     * @param request
     * @return
     */
//    @PostMapping("/add")
//    public BaseResponse<Long> addUser(@RequestBody UserAddRequest userAddRequest, HttpServletRequest request) {
//        if (userAddRequest == null) {
//            throw new BusinessException(ErrorCode.PARAMS_ERROR);
//        }
//        User user = new User();
//        BeanUtils.copyProperties(userAddRequest, user);
//        boolean result = userService.save(user);
//        if (!result) {
//            throw new BusinessException(ErrorCode.OPERATION_ERROR);
//        }
//        return ResultUtils.success(user.getId());
//    }

    /**
     * 删除用户
     *
     * @param deleteRequest
     * @param request
     * @return
     */
//    @PostMapping("/delete")
//    public BaseResponse<Boolean> deleteUser(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
//        if (deleteRequest == null || deleteRequest.getId() <= 0) {
//            throw new BusinessException(ErrorCode.PARAMS_ERROR);
//        }
//        boolean b = userService.removeById(deleteRequest.getId());
//        return ResultUtils.success(b);
//    }

    /**
     * 更新用户
     *
     * @param userUpdateRequest
     * @param request
     * @return
     */
//    @PostMapping("/update")
//    public BaseResponse<Boolean> updateUser(@RequestBody UserUpdateRequest userUpdateRequest, HttpServletRequest request) {
//        if (userUpdateRequest == null || userUpdateRequest.getId() == null) {
//            throw new BusinessException(ErrorCode.PARAMS_ERROR);
//        }
//        User user = new User();
//        BeanUtils.copyProperties(userUpdateRequest, user);
//        boolean result = userService.updateById(user);
//        return ResultUtils.success(result);
//    }

    /**
     * 获取用户列表
     *
     * @param userQueryRequest
     * @param request
     * @return
     */
//    @GetMapping("/list")
//    public BaseResponse<List<UserVO>> listUser(UserQueryRequest userQueryRequest, HttpServletRequest request) {
//        User userQuery = new User();
//        if (userQueryRequest != null) {
//            BeanUtils.copyProperties(userQueryRequest, userQuery);
//        }
//        QueryWrapper<User> queryWrapper = new QueryWrapper<>(userQuery);
//        List<User> userList = userService.list(queryWrapper);
//        List<UserVO> userVOList = userList.stream().map(user -> {
//            UserVO userVO = new UserVO();
//            BeanUtils.copyProperties(user, userVO);
//            return userVO;
//        }).collect(Collectors.toList());
//        return ResultUtils.success(userVOList);
//    }
    /**
     * 分页获取用户列表
     *
     * @param userQueryRequest
     * @param request
     * @return
     */
//    @GetMapping("/list/page")
//    public BaseResponse<Page<UserVO>> listUserByPage(UserQueryRequest userQueryRequest, HttpServletRequest request) {
//        long current = 1;
//        long size = 10;
//        User userQuery = new User();
//        if (userQueryRequest != null) {
//            BeanUtils.copyProperties(userQueryRequest, userQuery);
//            current = userQueryRequest.getCurrent();
//            size = userQueryRequest.getPageSize();
//        }
//        QueryWrapper<User> queryWrapper = new QueryWrapper<>(userQuery);
//        Page<User> userPage = userService.page(new Page<>(current, size), queryWrapper);
//        Page<UserVO> userVOPage = new PageDTO<>(userPage.getCurrent(), userPage.getSize(), userPage.getTotal());
//        List<UserVO> userVOList = userPage.getRecords().stream().map(user -> {
//            UserVO userVO = new UserVO();
//            BeanUtils.copyProperties(user, userVO);
//            return userVO;
//        }).collect(Collectors.toList());
//        userVOPage.setRecords(userVOList);
//        return ResultUtils.success(userVOPage);
//    }
}
